# -*- coding: utf-8 -*-
import logging

from fastapi import FastAPI
from starlette.websockets import WebSocket

logger = logging.getLogger(__name__)

ws_api = FastAPI()


@ws_api.websocket("/echo")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    while True:
        try:
            text = await websocket.receive_text()
            logger.info(f'ws receive:{text}')
            await websocket.send_text(f"ws receive:{text}")
        except Exception as e:
            logger.warning(f'ws error:{repr(e)}', exc_info=True)
            break
        pass
    pass
